/*

Copyright 2009 Nicola Dal Pont

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Version ${MAJOR_VERSION}.${MINOR_VERSION}${BETA_MARK} Build ${BUILD_NUMBER}

*/

package com.astoolkit.xmlmapping
{
   import flash.events.EventDispatcher;
   
   import mx.core.ClassFactory;
   import mx.core.IFactory;
   import mx.events.PropertyChangeEvent;
   import mx.formatters.Formatter;
   
   [DefaultProperty("mapping")]
   [Event(name="beforePropertyMap", type="com.astoolkit.xmlmapping.XmlMapPropertyEvent")]
   [Event(name="parse", type="com.astoolkit.xmlmapping.XmlMapPropertyParseEvent")]
   public class MapProperty extends EventDispatcher
   {
      XMLMAPPING_PRIVATE var _parentMapping : XmlMapping;
      
      /**
      * property name of the object being created.
      */
      public var targetProperty : String;
      
      /**
      * XPath expression pointing to the xml node to be evaluated
      */
      public var source : String = ".";
      
      /**
      * if false, no exception will be thrown when the source expression 
      * cannot be resolved. 
      * An exception is thrown if destination property does not exist. 
      */
      public var mandatory : Boolean = true;
      
      /**
      * an IXmlMappingParser class to be used for custom parsing of complex objects such as Date.
      * 
      * 
      * If parser is the current mapping's class object, the mapping object will be used as a parser. 
      * This assumes that the mapping object implements IXmlMappingParser
      */
      public var parserClass : IFactory = null;
      
      /**
      * a dictionary of parameters to be used by the parser:
      * for instance, a date parser might need an expected format string
      */
      public var parserContext : Object;
      /**
      * a formatter object to be used to format parsed data in a target property of type String.
      */ 
      public var formatter : Formatter;
      
      /**
      * specifies the target property's type. 
      * 
      * Used to perform mappings against untyped objects ( Flash's Object ). Note that in this scenario,
      * arrays are automatically created for source expressions resolving to multiple nodes.
      * If not defined, this targetProperty is mapped as String.
      * Ignored elsewhere. 
      */ 
      public var targetPropertyClass : IFactory = new ClassFactory( String );
      
	  /**
	   * an array of ClassSelector for deciding target class at runtime
	   * according through xpath expressions executed on the parsed xml 
	   */
	  [ArrayElementType("com.astoolkit.xmlmapping.ClassSelector")]
	  public var targetClassSelectors : Array;
	  
      public var targetClass : IFactory
      
      
      /**
      * specifies the XmlMapping class or class instance to be used to map custom classes objects, 
      * Arrays and ArrayCollections
      */
      public var mapping : Object;
      
      public var version : String = "*";
      
      public function get value( ) : *
      {
         return XMLMAPPING_PRIVATE::_parentMapping.context.getPropertyValue( this );
      }
      
      public function set value( inValue : Object ) : void
      {
   	   dispatchEvent( new PropertyChangeEvent( PropertyChangeEvent.PROPERTY_CHANGE, false, false, null, "value", null, inValue ) );
      }
	  
	  public var defaultRawValue : String;
   }
}